package com.woniuxy.qiantai.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.qiantai.entity.User;
import com.woniuxy.qiantai.mapper.UserMapper;
import com.woniuxy.qiantai.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void reg(String username, String password, String repass, String email, String tel) {
        synchronized (username.intern()) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", username);
            User user = userMapper.selectOne(queryWrapper);
            if (user != null) {
                throw new RuntimeException("用户名已被占用");
            }

            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("email", email);
            user = userMapper.selectOne(queryWrapper);
            if (user != null) {
                throw new RuntimeException("邮箱被占用");
            }

            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tel", tel);
            user = userMapper.selectOne(queryWrapper);
            if (user != null) {
                throw new RuntimeException("电话被占用");
            }

            //password非空,repass非空且与password一致
            if (StringUtils.isEmpty(password) || StringUtils.isEmpty(repass) || !password.equals(repass)) {
                throw new RuntimeException("两次输入密码不一致");
            }

            //校验通过,可以注册用户.
            User regUser = new User();

            regUser.setName(username);

            regUser.setPassword(password);

            regUser.setEmail(email);

            regUser.setTel(tel);

            userMapper.insert(regUser);
        }

    }
    public boolean checkNameExist(String name) {
        synchronized (name.intern()) {
            boolean flag = false;
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            User user = userMapper.selectOne(queryWrapper);
            if (user!=null){
                flag=true;
            }
            return flag;
        }
    }


    @Override
    public void loginCheck(String userName, String password) {

        //根据用户名查询用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", userName);

        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new RuntimeException("用户名错误");
        }

        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }

    }

    @Override
    public User getUserByUserName(String account) {
        //根据用户名查询用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", account);

        User user = userMapper.selectOne(queryWrapper);

        return user;
    }

    @Override
    public User getUserByAccount(String account) {
        //张朝朝：通过账户获取用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public List<User> selectUserNameByIds(List<Integer> userId) {
        List<User> users = userMapper.selectBatchIds(userId);
        return users;
    }


}
